package com.jml.account.shiroConfig;

import com.jml.account.common.entity.utils.StringUtil;
import com.jml.account.sysMenu.entity.SysMenu;
import com.jml.account.sysMenu.service.SysMenuService;
import com.jml.account.sysUser.entity.SysUser;
import com.jml.account.sysUser.service.SysUserService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm {

	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysMenuService sysMenuServiceImpl;
	/**
	 * 为当限前登录的用户授予角色和权
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		// 因为非正常退出，即没有显式调用 SecurityUtils.getSubject().logout()
		// (可能是关闭浏览器，或超时)，但此时缓存依旧存在(principals)，所以会自己跑到授权方法里。
		if (!SecurityUtils.getSubject().isAuthenticated()) {
			doClearCache(principals);
			SecurityUtils.getSubject().logout();
			return null;
		}
		SysUser user=(SysUser)principals.getPrimaryPrincipal();
		SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
		/*authorizationInfo.setRoles(userService.getRoles(userName));
		authorizationInfo.setStringPermissions(userService.getPermissions(userName));*/
		authorizationInfo.setRoles(user.getRoleList());
		authorizationInfo.setStringPermissions(user.getPermissionList());
		return authorizationInfo;
	}

	/**
	 * 验证当前登录的用户
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String userName=(String)token.getPrincipal();
		SysUser user=sysUserService.getByLoginName(userName);
		//-------------------------------------
		user.setMeauList(new ArrayList());


		Set<String> permissionSet= new HashSet();
		permissionSet.add("item:query");
		//permissionSet.add("item:edit");
		user.setPermissionList(permissionSet);


		user.setRoleList(new HashSet<String>());

		//----------------------------------------
		List<SysMenu> queryAllMenu = sysMenuServiceImpl.getAllPermission();
		SysMenu menuRoot =new SysMenu();
		Map<String,SysMenu> map = new HashMap<String,SysMenu>();
		int i = 0;
		for (SysMenu innerMenu : queryAllMenu) {
			innerMenu.setIndex(i+++"");
			map.put(innerMenu.getId(), innerMenu);

		}
		for (SysMenu sysMenu : queryAllMenu) {
			//通过子查找父
			//子菜单
			SysMenu child = sysMenu ; //假设为子菜单
			if(child.getPid() == null ){
				menuRoot = sysMenu;
			}else{
				//父节点
				SysMenu parent = map.get(child.getPid());
				parent.getChildren().add(child);
			}
		}
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		session.setAttribute("permissionRoot", menuRoot);
		//---------------------------------------
		if(user!=null){
			AuthenticationInfo authcInfo=new SimpleAuthenticationInfo(user,user.getPassword(),ByteSource.Util.bytes("salt"), this.getName());
			return authcInfo;
		}else{
			return null;
		}
	}


	//清除缓存
	public void clearCached() {
		PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
		super.clearCache(principals);
	}
}
